

module led_ctrl#(
    parameter		RUN_CYCLE   = 1000_000,     //us
    parameter		BLUE_CYCLE  = 100_000,      //us
    parameter       CLK_FREQ    = 100           //Mhz    
)(
	input	clk,
	input	rst_n,

	output	run_led,
    output  blue_led
);

reg [$clog2(CLK_FREQ)-1:0]      cnt_1us;
reg [$clog2(RUN_CYCLE)-1:0]     run_cnt;
reg [$clog2(BLUE_CYCLE)-1:0]    blue_cnt;

wire            pulse_1us;

assign  pulse_1us   = (cnt_1us == CLK_FREQ - 1'b1);
assign	run_led     = breath_led;//run_cnt   > (RUN_CYCLE >> 1);
assign	blue_led    = blue_cnt  > (BLUE_CYCLE >> 1);

/*----------pulse 1us----------*/
always @ (posedge clk or negedge rst_n)begin
	if(!rst_n)
        cnt_1us <= 8'd0;	   	   
    else if(pulse_1us)	   
        cnt_1us <= 1'b0;	    
    else 
        cnt_1us <= cnt_1us + 1'd1;	
end	

always @ (posedge clk or negedge rst_n)begin
	if(!rst_n)
		run_cnt <= 'd0;
	else if(run_cnt == RUN_CYCLE)
		run_cnt <= 'd0;
	else if(pulse_1us)
		run_cnt <= run_cnt + 1'b1;
end

always @ (posedge clk or negedge rst_n)begin
	if(!rst_n)
		blue_cnt <= 'd0;
	else if(blue_cnt == BLUE_CYCLE)
		blue_cnt <= 'd0;
	else if(pulse_1us)
		blue_cnt <= blue_cnt + 1'b1;
end

/*----------2s breath led----------*/
reg     [9:0]   cnt_1ms;
reg     [9:0]   cnt_1s;
reg             flag;
reg             breath_led;

wire            pulse_1ms;
wire            pulse_1s;

assign pulse_1ms    = cnt_1ms   == 999;
assign pulse_1s     = cnt_1s    == 999;


always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		cnt_1ms <= 'd0;
	else if(pulse_1ms)
		cnt_1ms <= 'd0;
	else if(pulse_1us)
		cnt_1ms <= cnt_1ms + 1'b1;
end

always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		cnt_1s <= 'd0;
	else if(pulse_1s)
		cnt_1s <= 'd0;
	else if(pulse_1ms)
		cnt_1s <= cnt_1s + 1'b1;
end

always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		flag <= 1'b0;
	else if(pulse_1s)
		flag <= ~flag;
	else 
		flag <= flag;
end

always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		breath_led <= 1'b0;
	else if(cnt_1s > cnt_1ms && flag)
		breath_led <= 1'b1;
    else if(cnt_1s < cnt_1ms && flag == 1'b0)
		breath_led <= 1'b1;
    else if(cnt_1s < cnt_1ms && flag)
		breath_led <= 1'b0;
    else if(cnt_1s > cnt_1ms && flag == 1'b0)
		breath_led <= 1'b0;
	else 
		breath_led <= breath_led;
end

endmodule